library("boot")
library(foreign)
library(plyr)

setwd("~/Dropbox/overton/pobe final/replication")
df<-read.dta("replication_main.dta")
pos<-read.dta("replication_main_long.dta")
setwd("~/Desktop/research/overton/draft")

a<-structable(median~study_issue+object+condition, data=qqq) 

## SEM MEDIAN ####

bmedian <- function(x, d) {
  return(median(x = x[d],  na.rm=T ))
}

qqq <- ddply(pos, .(study_issue, object, condition), summarize, median = median(x=pos_, na.rm=T),
             lb = quantile(boot(pos_, bmedian, R = 1000)$t, .01,na.rm = TRUE),
             ub = quantile(boot(pos_, bmedian, R = 1000)$t, .99, na.rm =TRUE),
             llb=quantile(pos_, .25,na.rm = TRUE ),uub=quantile(pos_, .75,na.rm = TRUE ))


### FIGURE 1 ###



par(bg="white", cex=1, mar=c(2,3,3,3), mfrow=c(3,2),oma=c(3,0,0,0))
c<-c(2,1,3)
with(subset(qqq, object=="le" & study_issue==1), plot( median, c, xlim=c(0,1),ylim=c(.5,3.5), pch=16, main="Immigration (Study 1)",
                                                       cex=1.4, lwd=2, xaxt="n", yaxt="n", xlab="Liberal - Conservative scale (0-1)", ylab=""))
with(subset(qqq, object=="lm" & study_issue==1), points( median, c, xlim=c(0,1), pch=1, cex=1.4, lwd=2))
with(subset(qqq, object=="rm" & study_issue==1), points( median, c, xlim=c(0,1), pch=0, cex=1.4, lwd=2))
with(subset(qqq, object=="re" & study_issue==1), points( median, c, xlim=c(0,1), pch=15, cex=1.4, lwd=2))
axis(4, at = c(1:3), labels=c("  Ext Lib", "  Control", " Ext Con"),  tick = FALSE,  outer = F, las=1, cex.axis=1.2)
axis(1, at =  c(0,0.5,1),  tick = TRUE,  outer = F)
abline(h=1:3, lty=3, lwd=1.3)

with(subset(qqq, study_issue==1 & object!="own"), segments( lb, c, ub, xlim=c(0,1), pch=15, cex=1.4, lwd=2))


with(subset(qqq, object=="le" & study_issue==2), plot( median, c, xlim=c(0,1),ylim=c(.5,3.5), pch=16, main="Welfare (Study 1)",
                                                       cex=1.4, lwd=2, xaxt="n", yaxt="n", xlab="Liberal - Conservative scale (0-1)", ylab=""))
with(subset(qqq, object=="lm" & study_issue==2), points( median, c, xlim=c(0,1), pch=1, cex=1.4, lwd=2))
with(subset(qqq, object=="rm" & study_issue==2), points( median, c, xlim=c(0,1), pch=0, cex=1.4, lwd=2))
with(subset(qqq, object=="re" & study_issue==2), points( median, c, xlim=c(0,1), pch=15, cex=1.4, lwd=2))
axis(1, at =  c(0,0.5,1),  tick = TRUE,  outer = F)
abline(h=1:3, lty=3, lwd=1.3)
with(subset(qqq, study_issue==2 & object!="own"), segments( lb, c, ub, xlim=c(0,1), pch=15, cex=1.4, lwd=2))


with(subset(qqq, object=="le" & study_issue==3), plot( median, c, xlim=c(0,1),ylim=c(.5,3.5), pch=16, main="Immigration (Study 2)",
                                                       cex=1.4, lwd=2, xaxt="n", yaxt="n", xlab="Liberal - Conservative scale (0-1)", ylab=""))
with(subset(qqq, object=="lm" & study_issue==3), points( median, c, xlim=c(0,1), pch=1, cex=1.4, lwd=2))
with(subset(qqq, object=="rm" & study_issue==3), points( median, c, xlim=c(0,1), pch=0, cex=1.4, lwd=2))
with(subset(qqq, object=="re" & study_issue==3), points( median, c, xlim=c(0,1), pch=15, cex=1.4, lwd=2))
axis(4, at = c(1:3), labels=c("  Ext Lib", "  Control", "  Ext Con"),  tick = FALSE,  outer = F, las=1, cex.axis=1.2)
axis(1, at =  c(0,0.5,1),  tick = TRUE,  outer = F)
abline(h=1:3, lty=3, lwd=1.3)
with(subset(qqq, study_issue==3 & object!="own"), segments( lb, c, ub, xlim=c(0,1), pch=15, cex=1.4, lwd=2))


with(subset(qqq, object=="le" & study_issue==4), plot( median, c, xlim=c(0,1), pch=16, main="Welfare (Study 2)",ylim=c(.5,3.5),
                                                       cex=1.4, lwd=2, xaxt="n", yaxt="n", xlab="Liberal - Conservative scale (0-1)", ylab=""))
with(subset(qqq, object=="lm" & study_issue==4), points( median, c, xlim=c(0,1), pch=1, cex=1.4, lwd=2))
with(subset(qqq, object=="rm" & study_issue==4), points( median, c, xlim=c(0,1), pch=0, cex=1.4, lwd=2))
with(subset(qqq, object=="re" & study_issue==4), points( median, c, xlim=c(0,1), pch=15, cex=1.4, lwd=2))
axis(1, at =  c(0,0.5,1),  tick = TRUE,  outer = F)
abline(h=1:3, lty=3, lwd=1.3)
with(subset(qqq, study_issue==4 & object!="own"), segments( lb, c, ub, xlim=c(0,1), pch=15, cex=1.4, lwd=2))

c<-c(-1,2,1,3)

with(subset(qqq, object=="le" & study_issue==5), plot( median, c, xlim=c(0,1),ylim=c(.5,3.5), pch=16, main="Abortion (Study 3)",
                                                       cex=1.4, lwd=2, xaxt="n", yaxt="n", xlab="Liberal - Conservative scale (0-1)", ylab=""))
with(subset(qqq, object=="lm" & study_issue==5), points( median, c, xlim=c(0,1), pch=1, cex=1.4, lwd=2))
with(subset(qqq, object=="rm" & study_issue==5), points( median, c, xlim=c(0,1), pch=0, cex=1.4, lwd=2))
with(subset(qqq, object=="re" & study_issue==5), points( median, c, xlim=c(0,1), pch=15, cex=1.4, lwd=2))
axis(4, at = c(1:3), labels=c("  Ext Lib", "  Control", " Ext Con"),  tick = FALSE,  outer = F, las=1, cex.axis=1.2)
axis(1, at =  c(0,0.5,1),  tick = TRUE,  outer = F)
abline(h=1:3, lty=3, lwd=1.3)
with(subset(qqq, study_issue==5 & object!="own"), segments( lb, c, ub, xlim=c(0,1), pch=15, cex=1.4, lwd=2))

with(subset(qqq, object=="le" & study_issue==6), plot( median, c, xlim=c(0,1),ylim=c(.5,3.5), pch=16, main="Minimum wage (Study 3)",
                                                       cex=1.4, lwd=2, xaxt="n", yaxt="n", xlab="Liberal - Conservative scale (0-1)", ylab=""))
with(subset(qqq, object=="lm" & study_issue==6), points( median, c, xlim=c(0,1), pch=1, cex=1.4, lwd=2))
with(subset(qqq, object=="rm" & study_issue==6), points( median, c, xlim=c(0,1), pch=0, cex=1.4, lwd=2))
with(subset(qqq, object=="re" & study_issue==6), points( median, c, xlim=c(0,1), pch=15, cex=1.4, lwd=2))
axis(1, at =  c(0,0.5,1),  tick = TRUE,  outer = F)
abline(h=1:3, lty=3, lwd=1.3)
with(subset(qqq, study_issue==6 & object!="own"), segments( lb, c, ub, xlim=c(0,1), pch=15, cex=1.4, lwd=2))

mtext("Perceived position (Extremely liberal - Extremely conservative)", side=1, line=1, outer=TRUE, cex=1, font=1)


### FIGURE S3

exps<-c("Immigration (Study 1)", 
        "Welfare (Study 1)",
        "Immigration (Study 2)",
        "Welfare (Study 2)",
        "Abortion (Study 3)",
        "Minimum wage (Study 3)")
par(mfrow=c(3,2), mar=c(3,1,4,2), oma=c(5,0,0,0))

for (i in 1:6)
{
  ddf<-subset(df, condition!="Both" & study_issue==i)
  ddf<-subset(ddf, !is.na(first))
  
  a<-with(ddf, table( first, condition))
  b<-t(t(a)/colSums(a))
  b<-b[,c(2,1,3)]
  barplot(b, axes=F, main=exps[i], col=c("white", "grey70" ,"grey40","black"), cex.names=1.5, cex.main=1.5,
          names.arg=c("Ext Lib", "Control", "Ext Con"))
  
}

### FIGURE S4


loess <- loess(lib ~ pos_own, df, subset=(condition=="Control"), span=1)
q<-predict(loess, data.frame(pos_own = seq(0, 1, .01)), se = TRUE)
loess <- loess(lib ~ rel_dist, df, subset=(condition=="Control"), span=1)
p<-predict(loess, data.frame(rel_dist = seq(-1, 1, .01)), se = TRUE)


par(mar=c(4,0,3,1), mfrow=c(1,2), oma=c(1,5,1,0.5))
plot(seq(0, 1, .01),q$fit, type="l", ylim=c(-0.01,1), lwd=3, yaxt="n", cex.main=1,
     xlab="Own position (liberal-conservative)", main="A) Policy preference \nby own position")
axis(side=2, at=c(0,.25,.5,.75,1), labels=T, las=1)
title( ylab="P(Chose ML over MC)", outer=T)
points(seq(0, 1, .01),q$fit-1.96*q$se, type="l", lty=2, lwd=3)
points(seq(0, 1, .01),q$fit+1.96*q$se, type="l", lty=2, lwd=3.)


plot(seq(-1, 1, .01),p$fit, type="l", ylim=c(-.1,1.1), lwd=3, yaxt="n", cex.main=1,
     xlab="Relative proximity of ML", , main="B) Policy preference \nby relative proximity")
points(seq(-1, 1, .01),p$fit-1.96*p$se, type="l", lty=2, lwd=3)
points(seq(-1, 1, .01),p$fit+1.96*p$se, type="l", lty=2, lwd=3)

